﻿using log4net;
using MyWebApp.Common;
using MyWebApp.Common.UIAttribute;
using MyWebApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MyWebApp.Controllers
{
    //指定session 只读
    [SessionState(System.Web.SessionState.SessionStateBehavior.Default)]
    public class SecurityController : BaseMvcController
    {
        private ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        // GET: Security
        [AllowAnonymous]
        public ActionResult Login(string ReturnUrl)
        {
            if (CurrentUser != null)
            {
                CurrentUser = null;
            }
            var model = new LoginUserModel() { ReturnUrl = ReturnUrl };
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(LoginUserModel model)
        {
            var result = userLogin(model);
            if (result == "OK")
            {
                if (!string.IsNullOrEmpty(model.ReturnUrl))
                {
                    return Redirect(model.ReturnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Admin");
                }
            }
            else
            {
                ViewBag.ErrorMsg = result;
                return View();
            }
        }

        private string userLogin(LoginUserModel model)
        {
            try
            {
                if (model!= null && AuthUtil.doLogin(model.UserId, model.Password, model.RememberMe))
                {
                    log.Info("用户" + CurrentUser.UserId + "登录成功。");
                    return "OK";
                }
                else
                {
                    return "用户名或密码错误";
                }
            }
            catch (Exception ex)
            {
                log.Error("登录失败", ex);
                return string.Format("登录失败", ex.Message);
            }
        }

        [HttpGet]
        public ActionResult LogOut()
        {
            if (CurrentUser != null)
            {
                log.DebugFormat("用户{0}登出", CurrentUser.UserId);

            }
            //清除退出票据
            FormsAuthentication.SignOut();
            Session.Abandon();
            //Request.Cookies.Clear();
            return RedirectToAction("Login", "Security");
        }

        [HttpGet]
        public ActionResult LockScreen(string ReturnUrl)
        {
            var model = CurrentUser;
            model.LockSession = true;
            model.ReturnUrl = ReturnUrl;
            return View(model);
        }

        [HttpPost]
        public ActionResult LockScreen(LoginUserModel model)
        {
            model.UserId = CurrentUser.UserId;//当前用户
            model.UserName = CurrentUser.UserName;
            var result = userLogin(model);
            if (result == "OK")
            {
                model.LockSession = false;
                if (string.IsNullOrEmpty(model.ReturnUrl))
                {
                    return RedirectToAction("Index", "Admin");
                }
                else
                {
                    return Redirect(model.ReturnUrl);
                }
            }
            else
            {
                ViewBag.ErrorMsg = result;
                return View(model);
            }
        }
    }
}